<?php
// +----------------------------------------------------------------------
// | 德力PHP框架 [ DeLiPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2019~2050 天津德力仪器设备有限公司 [ http://www.deviser.com.cn ]
// +----------------------------------------------------------------------
// | 官方网站: http://www.deviser.com.cn
// +----------------------------------------------------------------------

namespace app\admin\controller;

use app\common\builder\ZBuilder;
use app\user\model\Message as MessageModel;
use app\user\model\Role as RoleModel;
use app\user\model\User as UserModel;

/**
 * 消息控制器
 * @package app\admin\controller
 */
class Message extends Admin
{
    /**
     * 消息中心
     * @return mixed
     * @throws \think\Exception
     * @throws \think\exception\DbException
     * @author deli <123456789@qq.com>
     */
    public function index()
    {
        $data_list = MessageModel::where($this->getMap())
            ->where('uid_receive', UID)
            ->order($this->getOrder('id DESC'))
            ->paginate();

        return ZBuilder::make('table')
            ->setTableName('admin_message')
            ->addTopButton('enable', ['title' => '设置已阅读'])
            ->addTopButton('disable', ['title' => '设置未阅读'])
            ->addTopButton('delete')
            ->addRightButton('detail', ['title' => '查看详情', 'icon' => 'fa fa-fw fa-search', 'href' => url('detail', ['id' => '__id__'])])
            ->addRightButton('enable', ['title' => '设置已阅读'])
            ->addRightButton('disable', ['title' => '设置未阅读'])
            ->replaceRightButton(['status' => 1], '', 'enable')
            ->replaceRightButton(['status' => 0], '', 'disable')
            ->addRightButton('delete')
            ->addColumns([
                ['uid_send', '发送者', 'callback', 'get_nickname'],
                ['type', '分类'],
                ['content', '内容'],
                ['status', '状态', 'status', '', ['未读', '已读']],
                ['create_time', '发送时间', 'datetime'],
                ['read_time', '阅读时间', 'datetime'],
                ['right_button', '操作', 'btn'],
            ])
            ->addFilter('type')
            ->addFilter('status', ['未读', '已读'])
            ->setRowList($data_list)
            ->fetch();
    }

    /**
     * 信件详情
     * @param null $id
     * @return mixed
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function detail($id = null)
    {
        if ($id === null) {
            $this->error('缺少参数');
        }

        if ($this->request->isPost()) {
            $ids = $this->request->param('id');
            $map = [
                ['uid_receive', '=', UID],
                ['id', '=', $ids]
            ];
            $result = MessageModel::where($map)
                ->update(['status' => 1, 'read_time' => $this->request->time()]);
            if (false !== $result) {
                $this->success('设置成功', 'index', '_parent_reload');
            } else {
                $this->error('设置失败');
            }
        }
        $info = MessageModel::get($id);
        return ZBuilder::make('form')
            ->addFormItems([
                ['hidden', 'id'],
                ['text', 'type', '消息分类', '', '', '', 'readonly'],
                ['textarea', 'content', '消息内容', '', '', 'readonly'],
            ])
            ->setBtnTitle('submit', '已读')
            ->setFormData($info)
            ->fetch();
    }

    /**
     * 设置已阅读
     * @param array $ids
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     * @author deli <123456789@qq.com>
     */
    public function enable($ids = [])
    {
        empty($ids) && $this->error('参数错误');
        $map = [
            ['uid_receive', '=', UID],
            ['id', 'in', $ids]
        ];
        $result = MessageModel::where($map)
            ->update(['status' => 1, 'read_time' => $this->request->time()]);
        if (false !== $result) {
            $this->success('设置成功');
        } else {
            $this->error('设置失败');
        }
    }

    /**
     * 设置未阅读
     * @param array $ids
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     * @author deli <123456789@qq.com>
     */
    public function disable($ids = [])
    {
        empty($ids) && $this->error('参数错误');
        $map = [
            ['uid_receive', '=', UID],
            ['id', 'in', $ids]
        ];
        $result = MessageModel::where($map)
            ->update(['status' => 0]);
        if (false !== $result) {
            $this->success('设置成功');
        } else {
            $this->error('设置失败');
        }
    }
}